#!/bin/sh

revision="copy FEC/EL v0.2.0 (2023-07-09 by MIBonk)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

	case $1 in

-el) {

	$thisdir/mounts -w

	checksum0="9089e2500e0faa8dc02c0e72b6e8b39ef50c220e" #STOCK MHIG/MHI2
	checksum1="57e9e0a543f4f0aa80e48eb1c8de2500cbd8cba6" #RSDB

		checksum_unit="$($SHA1 /net/rcc/mnt/efs-persist/FEC/ExceptionList.txt | awk '{print $1}')" 2>> $LOG
		echo "--- Checking if stock EL is installed" | $TEE -a $LOG
		if [ $checksum0 = $checksum_unit ] || [ $checksum1 = $checksum_unit ]; then
			echo "    Stock EL found on unit" | $TEE -a $LOG
		else
			echo -ne "-- Custom EL found - (sha1=$checksum_unit)\n    Copy stock EL back to unit" | $TEE -a $LOG
			if [[ "$TRAINVERSION" = *MHIG* ]]; then
				cp -rf $VOLUME/patches/EL/ExceptionList.txt /net/rcc/mnt/efs-persist/FEC/ExceptionList.txt 2>> $LOG
			else
				cp -rf $VOLUME/patches/EL/ExceptionList_RSDB.txt /net/rcc/mnt/efs-persist/FEC/ExceptionList.txt 2>> $LOG
			fi
			ERROR
			echo "--- EL is restored" | $TEE -a $LOG
		fi

	[ -z "$GEM" ] && echo -ne "\n"

	$thisdir/mounts -r
	return 2> /dev/null

};;


-fec) {

	$thisdir/mounts -w

	# generate FEC

	. $thisdir/addfec -a

	# copy FEC to Unit, if no one in patch directory check backup folder
	if [ ! -s /net/rcc/mnt/efs-persist/FEC/FecContainer.fec ];then
		echo -ne "!!! FEC file NOT found in /net/rcc/mnt/efs-persist/FEC/\n    Try to restore from backup\n" | $TEE -a $LOG
		[ -n "$GEM" ] && echo -ne "\n"
		cp -rf $BACKUPFOLDER/FEC/FecContainer.fec /net/rcc/mnt/efs-persist/FEC/FecContainer.fec 2>> $LOG
		if [ -s /net/rcc/mnt/efs-persist/FEC/FecContainer.fec ];then
			echo -ne "--- Restore Done!" | $TEE -a $LOG
		else
			echo -ne "!!! Restore Failed!\n    No FecContainer.fec found in SD backup\n" | $TEE -a $LOG
		fi
	fi

	[ -z "$GEM" ] && echo -ne "\n"

	$thisdir/mounts -r
	return 2> /dev/null

};;


-b) {

	$thisdir/mounts -w

	# copy FEC and EL from Backup to Unit
	if [ -s $BACKUPFOLDER/FEC/ExceptionList.txt ];then
		echo -ne "copy EL file from backup "$FAZIT"..." | $TEE -a $LOG
		cp -rf $BACKUPFOLDER/FEC/ExceptionList.txt /net/rcc/mnt/efs-persist/FEC/ExceptionList.txt 2>> $LOG
		ERROR
		echo -ne "\n" | $TEE -a $LOG
	else
		echo -ne "there no EL in backup!\n\n" | $TEE -a $LOG
	fi

	if [ -s $BACKUPFOLDER/FEC/FecContainer.fec ];then
		echo -ne "copy FEC file from backup "$FAZIT"..." | $TEE -a $LOG
		cp -rf $BACKUPFOLDER/FEC/FecContainer.fec /net/rcc/mnt/efs-persist/FEC/FecContainer.fec 2>> $LOG
		ERROR
		echo -ne "\n" | $TEE -a $LOG
	else
		echo -ne "there is no FEC in backup!\n\n" | $TEE -a $LOG
	fi

	$thisdir/mounts -r
	return 2> /dev/null

echo -ne "--- Please reboot for changes can take effect...\n" | $TEE -a $LOG

};;

-c) {

	$thisdir/mounts -w

	# copy FEC and EL from Backup to Unit
	echo -ne "--- Place custom ExceptionList.txt and/or custom FecContainer.fec\n    in SD /patches/. They will be copied to unit.\n" | $TEE -a $LOG
	if [ -s $VOLUME/patches/ExceptionList.txt ];then
		echo -ne "--- copy EL file from /patches/ExceptionList.txt..." | $TEE -a $LOG
		cp -rf $VOLUME/patches/ExceptionList.txt /net/rcc/mnt/efs-persist/FEC/ExceptionList.txt 2>> $LOG
		ERROR
	else
		echo -ne "!!! there no ExceptionList.txt in /patches/\n\n" | $TEE -a $LOG
	fi

	if [ -s $VOLUME/patches/FecContainer.fec ];then
		echo -ne "--- copy FEC file from /patches/FecContainer.fec..." | $TEE -a $LOG
		cp -rf $VOLUME/patches/FecContainer.fec /net/rcc/mnt/efs-persist/FEC/FecContainer.fec 2>> $LOG
		ERROR
	else
		echo -ne "!!! there is no FecContainer.fec in /patches/\n\n" | $TEE -a $LOG
	fi

	$thisdir/mounts -r
	return 2> /dev/null

echo -ne "--- Please reboot for changes can take effect...\n" | $TEE -a $LOG

};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION]"
		echo ""
		echo "Options:"
		echo "        -fec 			create FEC based on addFec.txt and copy to unit"
		echo "        -el 			restore stock EL to unit"
		echo "        -b 			copy FEC and EL from Backup to unit"
		echo "        -c 			copy custom FEC and EL from /patches/ to unit"
		echo "        --help			show this help"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
		
	};;

	esac

exit 0

